package com.day12.study.chat;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务端
 *
 * @author Twinkle
 */
public class Server {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8888);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("服务器已启动于8888端口");
        List<Socket> list = new ArrayList<>();
        int i = 1;
        while (true) {
            try {
                //在指定端口号监听，等待连接
                Socket socket = serverSocket.accept();
                list.add(socket);
                if (socket != null) {
                    System.out.println("用户" + i++ + "：" + socket.getRemoteSocketAddress());
                }
                new Thread(new ServerThread(socket, list)).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
